package online.flowerinsnow.minecraft.bukkit.testffa.manager;

import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

public class CombatManagerImpl implements ICombatManager {
    private Map<Player, Integer> combatTime = new HashMap<>();
    private Map<Player, Player> combatTarget = new HashMap<>();
    @Override
    public int getCombatTime(@NotNull Player player) {
        if (!player.isOnline()) return -1; // 玩家不在线 返回-1
        return combatTime.getOrDefault(player, 0); // 玩家在线 获取玩家剩余战斗时间
    }

    @Override
    public void setCombatTime(@NotNull Player player, int time) {
        if (time <= 0) {
            combatTime.remove(player); // 为了避免浪费资源堆积太多 如果设置时间<=0 直接删除
            if (player.isOnline()) { // 玩家在线 设置计分板
                ScoreboardManager.hideLine(player, 4);
            }
        } else {
            combatTime.put(player, time); // 设置玩家剩余战斗时间
            if (player.isOnline()) {
                ScoreboardManager.setLineSuffix(player, 4, "§c" + time);
            }
        }

    }

    @Override
    public @Nullable Player getCombatTarget(@NotNull Player player) {
        return combatTarget.get(player);
    }

    @Override
    public void setCombatTarget(@NotNull Player player, @Nullable Player target) {
        if (target == null) {
            combatTarget.remove(player);
            if (player.isOnline()) { // 玩家在线 设置计分板
                ScoreboardManager.hideLine(player, 3);
            }
        } else {
            combatTarget.put(player, target);
            if (player.isOnline()) {
                ScoreboardManager.setLineSuffix(player, 3, "§c" + target.getName());
            }
        }
    }
}
